package com.leigang.boot.redis.springbootredis.controller;

import com.leigang.boot.redis.springbootredis.entity.UserToken;
import com.leigang.boot.redis.springbootredis.service.RedisService;
import com.leigang.boot.redis.springbootredis.utils.ResponseUtils;
import com.leigang.boot.redis.springbootredis.utils.SnowUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class UserTokenController {

    private final RedisService redisService;
    public UserTokenController(RedisService redisService) {
        this.redisService = redisService;
    }

    @PostMapping("/addToken")
    public Map<String, Object> addToken(@RequestBody HashMap<String, String> map){
        UserToken userToken = new UserToken();
        userToken.setToken(SnowUtils.getId());
        redisService.setObj(userToken.getToken(),map.get("userId"));
        return ResponseUtils.success(userToken.getToken());
    }

    @PostMapping("/getToken")
    public Map<String, Object> getToken(@RequestBody HashMap<String, String> map){
        String obj = (String)redisService.getObj(map.get("token"));
        return ResponseUtils.success(obj);
    }

    @PostMapping("/delToken")
    public Map<String, Object> delToken(@RequestBody HashMap<String, String> map){
        Boolean aBoolean = redisService.delObj(map.get("token"));
        return ResponseUtils.success(aBoolean);
    }
}
